package cn.rslee.uaas.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.rslee.uaas.dto.AccessInfo;
import cn.rslee.uaas.entity.AccessDay;
import cn.rslee.uaas.repository.AccessDayRepository;
import cn.rslee.uaas.util.U;

/**
 * 描述：
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON                 REASON
 *  1    2016年3月3日             rslee               Create
 * ****************************************************************************
 * </pre>
 * @author rslee
 * @since 1.0
 */
@Component("accessDayService")
public class AccessDayServiceImpl implements AccessDayService{
	
	@Resource
	private AccessDayRepository accessDayRepository;

	@Override
	public List<AccessDay> findAll() {
		return accessDayRepository.findAll();
	}

	@Override
	public AccessDay get(long id) {
		return accessDayRepository.findOne(id);
	}

	@Override
	public AccessInfo findBySysCodeAndDay(String sysCode, String day) {
		Date tDay = U.toDate(day);
		Date lDay = U.addDay(tDay, -1);
		AccessDay thisDay = accessDayRepository.getByDaysAndSysCode(tDay, sysCode);
		AccessDay lastDay = accessDayRepository.getByDaysAndSysCode(lDay, sysCode);
		AccessInfo ai = new AccessInfo();
		if(thisDay != null){
			ai.setThisPv(thisDay.getPv());
			ai.setThisUv(thisDay.getUv());
			ai.setThisIp(thisDay.getIp());
		}
		if(lastDay != null){
			ai.setLastPv(lastDay.getPv());
			ai.setLastUv(lastDay.getUv());
			ai.setLastIp(lastDay.getIp());
		}
		return ai;
	}

}
